import 'package:animate_do/animate_do.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/common/app.dart';
import 'package:flutter_smart_heating/common/http.dart';
import 'package:flutter_smart_heating/config/config.dart';
import 'package:flutter_smart_heating/ui/ui.dart';
import 'package:get/get.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  bool showLoginPage = false;
  bool checking = true;
  @override
  initState() {
    enterSystem();
    super.initState();
  }

  checkState() async {
    var res = await Http.instance.post('/api/user/index/index');
    setState(() {
      checking = false;
    });
    if (res.code == 1) {
      Get.offNamed('/');
    } else {
      App.push('/login', redirect: true);
    }
  }

  enterSystem() async {
    await 3.5.delay();
    if (mounted) {
      var result = UiPolicyPopup.show(
        context,
        checked: true,
        onAgree: () {
          checkState();
        },
      );
      if (result) {
        checkState();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: const Color(0XFFDCE9FF),
      body: AnimatedSwitcher(
        duration: kThemeAnimationDuration,
        child: flashView(),
      ),
    );
  }

  Widget flashView() {
    return Stack(
      children: [
        Positioned.fill(
          child: FadeIn(
            child: Image.asset(
              'res/images/splash.png',
              fit: BoxFit.cover,
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: SafeArea(
            child: Column(
              children: [
                const SizedBox(height: kToolbarHeight * 2),
                Bounce(
                  child: Image.asset(
                    'res/images/launcher_icon_round.png',
                    height: 100,
                  ),
                ),
                const SizedBox(height: 25),
                FlipInX(
                  delay: const Duration(milliseconds: 1500),
                  child: const UiLinearGradientMask(
                    child: Text(
                      Config.appName,
                      style: TextStyle(
                        fontSize: 30,
                        fontWeight: FontWeight.bold,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ),
                const SizedBox(height: 8),
                JelloIn(
                  delay: const Duration(milliseconds: 2000),
                  child: const Text(
                    '温情供暖・和谐取暖',
                    style: TextStyle(
                      fontSize: 18,
                      color: Colors.black54,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: ZoomIn(
            delay: const Duration(milliseconds: 2000),
            child: Visibility(
              visible: checking,
              child: const CupertinoActivityIndicator(
                color: Colors.black,
              ),
            ),
          ),
        ),
        const Positioned(
          bottom: 30,
          left: 0,
          right: 0,
          child: Center(
            child: Text(
              '©2023 智慧供热 Inc. All Rights Reserved.',
              style: TextStyle(
                fontSize: 12,
                color: Colors.black54,
              ),
            ),
          ),
        )
      ],
    );
  }
}
